<template>
  <div class="parking-permission-guard">
    <!-- 权限不足时显示提示 -->
    <div v-if="!hasMenuPermission" class="permission-denied">
      <el-empty 
        description="非物业公司人员不能查看本页面数据"
        :image-size="200"
      >
        <template #description>
          <div class="permission-message">
            <p class="message-title">权限不足</p>
            <p class="message-content">非物业公司人员不能查看本页面数据</p>
            <p class="message-tip">请联系管理员获取停车管理权限</p>
          </div>
        </template>
        <el-button type="primary" @click="handleContactAdmin">
          联系管理员
        </el-button>
      </el-empty>
    </div>
    
    <!-- 有权限时显示内容 -->
    <div v-else class="permission-content">
      <slot></slot>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useUserStore } from '@/stores/user'
import { usePermissionStore } from '@/store/permission'
import { ROLE_CODES } from '@/constants/roles'
import { isSuperAdmin, hasPermission } from '@/utils/permission'
import { ElMessage } from 'element-plus'
import { showSuccess, showError, showWarning, showInfo } from '@/utils/message'

// 作者：Wu.Liang

const props = defineProps({
  // 菜单权限编码
  menuPermission: {
    type: String,
    default: 'parking:menu'
  }
})

const emit = defineEmits(['permission-denied', 'permission-granted'])

const userStore = useUserStore()
const permissionStore = usePermissionStore()
const loading = ref(true)

// 检查权限
const checkPermission = () => {
  // 使用统一的权限判断方法
  if (isSuperAdmin()) return true
  return hasPermission(props.menuPermission)
}

// 仅菜单权限判断
const hasMenuPermission = computed(() => {
  if (!userStore.userInfo) return false
  if (isSuperAdmin()) return true
  if (!userStore.userInfo.propertyCompanyId) return false
  return checkPermission()
})

// 联系管理员
const handleContactAdmin = () => {
  // 可以跳转到联系管理员页面或显示联系方式
  showInfo('请联系系统管理员获取停车管理权限')
}

onMounted(() => {
  loading.value = false
  if (hasMenuPermission.value) {
    emit('permission-granted')
  } else {
    emit('permission-denied', {
      type: 'no-menu-permission',
      message: '无菜单访问权限'
    })
  }
})
</script>

<style scoped>
.parking-permission-guard {
  width: 100%;
  height: 100%;
}

.permission-denied {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 400px;
  background-color: #fafafa;
}

.permission-message {
  text-align: center;
  margin-bottom: 20px;
}

.message-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin-bottom: 10px;
}

.message-content {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.message-tip {
  font-size: 12px;
  color: #999;
  margin-bottom: 0;
}

.permission-content {
  width: 100%;
  height: 100%;
}
</style> 